<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/8/7
 * Time: 10:32
 * 游记
 */
namespace app\admin\controller;
use app\common\model\InfoModel;
use app\common\model\TravelsModel;
use phpDocumentor\Reflection\DocBlock\Tags\Author;
use think\Db;

class Travels extends Base{
    public function index(){
        $where = [];
        if(input('nickname')){
            $nickname = input('nickname');
            $where['m.nickname']= ['like',"%$nickname%"];
        }
        if(input('address')){
            $city = input('address');
            $where['t.address'] = ['like',"%$city%"];
        }
        $data = TravelsModel::alias('t')->join('think_member m','m.id = t.member_id')->where('t.is_show',1)->where($where)->field('t.*')->paginate(10);
       $this->assign('data',$data);
      return $this->fetch();
    }
    //未审核
    public function no_pass(){
        $where = [];
        if(input('nickname')){
            $nickname = input('nickname');
            $where['m.nickname']= ['like',"%$nickname%"];
        }
        if(input('address')){
            $city = input('address');
            $where['t.address'] = ['like',"%$city%"];
        }
        $data = TravelsModel::alias('t')->join('think_member m','m.id = t.member_id')->where('t.is_show','<>',1)->where($where)->field('t.*')->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }
    //审核ajax
    public function is_pass(){
        if(request()->isAjax()){
            $id = input('id');
            $value = input('value');
            if(empty($id) || empty($value)) return json(['code'=>1,'data'=>'','msg'=>'参数错误']);
            $travelsmodel = TravelsModel::get($id);
            $travelsmodel->is_show = $value;
            $re = $travelsmodel->save();
            if($re){
                return json(['code'=>200,'data'=>$value,'msg'=>'更新成功']);
            }else{
                return json(['code'=>2,'data'=>'','msg'=>'更新失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //查询详情
    public function get_travel(){
        if(request()->isAjax()){
            $id = input('id');
            if(!$id) return json(['code'=>1,'data'=>'','msg'=>'参数错误']);
            $data = TravelsModel::get($id);
            return json(['code'=>200,'data'=>$data,'msg'=>'ok']);
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //删除
    public function del(){
        if(request()->isAjax()){
            $id = input('id');
            if(!$id) return json(['code'=>1,'data'=>'','msg'=>'参数错误']);
            $re = TravelsModel::del($id);
            if($re){
                return json(['code'=>200,'data'=>'','msg'=>'删除成功']);
            }else{
                return json(['code'=>2,'data'=>'','msg'=>'删除失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    public function test(){
        $re = TravelsModel::get(1);
        //详细信息
        echo "==========================详细信息====================<br>";
        if($re->info_id){
            dump($re->info_id->toArray());
            $info = $re->info_id;
            echo "==========================住宿信息====================<br>";
            foreach ($info->accommodation_id as $k=>$v){
                dump($v->toArray());
            }
            echo "==========================推荐信息====================<br>";
            foreach ($info->recommend_id as $k=>$v){
                dump($v->toArray());
            }
        }
        echo "==========================旅行记录====================<br>";
        if($re->travel_records_id){

            foreach ($re->travel_records_id as $k=>$v){
                if($v->img){
                    foreach ($v->img as $k1=>$v1){
                        dump($v1->toArray());
                        if(file_exists(ROOT_PATH.$v1['src'])){
                            unlink(ROOT_PATH.$v1['src']);
                        }
                    }
                }
//                dump($v->toArray());
            }
        }


    }
    public function test1(){
        Db::startTrans();
        try{
        $id = 1;
        $re = TravelsModel::get($id);
        $del_img = [];//删除的图片ids
        $del_travel_records_id=null;//旅行日记id
        $del_recommend_id = [];//推荐ids
        $del_accommodation_id = [];//住宿ids
        //详细信息id
        $del_info_id = null;
        //详细信息
        // echo "==========================详细信息====================<br>";
        if($re->info_id){
            $info = $re->info_id;
            $del_info_id = $info->id;
            //==========================住宿信息====================<br>";
            foreach ($info->accommodation_id as $k=>$v){
                $del_accommodation_id[] = $v->id;
            }
            foreach ($info->recommend_id as $k=>$v){
                $del_recommend_id[] = $v->id;
            }
        }
        // echo "==========================旅行记录====================<br>";
        if($re->travel_records_id){

            foreach ($re->travel_records_id as $k=>$v){
                if($v->img){
                    foreach ($v->img as $k1=>$v1){
                        //删除图片
                        if(file_exists(ROOT_PATH.$v1['src'])){
                            unlink(ROOT_PATH.$v1['src']);
                        }
                        $del_img[] = $v1->id;
                    }
                }
            }
        }
        // 启动事务

            //删除的图片ids
            if($del_img){
                Db::table('think_img')->delete($del_img);
            }
            //旅行日记id
            if($del_travel_records_id){
                Db::table('think_travel_records')->delete($del_travel_records_id);
            }
            //推荐ids
            if($del_recommend_id){
                Db::table('think_recommend')->delete($del_recommend_id);
            }
            //住宿ids
            if($del_accommodation_id){
                Db::table('think_accommodation')->delete($del_accommodation_id);
            }
            //详细信息
            if($del_info_id){
                Db::table('think_info')->delete($del_info_id);
            }
            //删除游记
            Db::table('think_travels')->delete($id);
            // 提交事务
            Db::commit();
            echo 'ok';
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            echo 'on';
        }
     

    }

}